# -*- coding: UTF-8 -*-
import os
import stat
from ls import Ls
from accessFile import  access
class Opfile():
    def __init__(self):
        pass

    def mkfile(self,path):
        access_ = access(path)
        if access_.isFile() and not access_.isDir():
            return self.getFileAbsolutePath(path)
        else:
            f = open(path,"w")
            f.close()
            return self.getFileAbsolutePath(path)

    def rmfile(self,path):
        access_ = access(path)
        if access_.isFile() and not access_.isDir():
            return os.remove(path)
        elif not access_.isFile():
            return True

    def getFileAbsolutePath(self,path):
        return os.path.abspath(path)

    def getFileName(self,path):
        return os.path.basename(path)

    def getFilePathName(self,path):
        return os.path.dirname(path)


    def ll(self,path):
        return Ls.listFile(path)

    def chmod(self,path,r = 0,w = 0,x= 0):
        stat_ = 0
        stats = 0
        if r :
            stat_ += 4
        if w :
            stat_ += 2
        if x :
            stat_ += 1
        print(stat_)
        if stat_ == 1:
            stats = stat.S_IXUSR
        elif stat_ == 2:
            stats = stat.S_IWUSR
        elif stat_ == 3:
            stats = stat.S_IWUSR | stat.S_IXUSR
        elif stat_ == 4:
            stats = stat.S_IRUSR
        elif stat_ == 6:
            stats = stat.S_IWUSR | stat.S_IRUSR
        elif stat_ == 7:
            stats = stat.S_IRWXU
        else:
            stats = stat.S_IRUSR


        return os.chmod(path,stats)



OpFile = Opfile()
FileCmd = OpFile
if __name__ == "__main__":
    OpFile.mkfile("test.py")

    print("1:",OpFile.getFileName("test.py"))
    print("2:",OpFile.getFileAbsolutePath("test.py"))
    print("3:",OpFile.getFilePathName("test.py"))

    print(OpFile.ll("test.py"))
    OpFile.chmod("test.py",0,0,0)
    print(OpFile.ll("test.py"))

    a = 0
    input(a)

    OpFile.rmfile("test.py")